package com.chd.ledger_server.service.impl;

import com.chd.ledger_server.entity.DevLedger;
import com.chd.ledger_server.dao.DevLedgerDao;
import com.chd.ledger_server.service.DevLedgerService;
import com.chd.ledger_server.domain.PageDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备检修台账表(DevLedger)表服务实现类
 *
 * @author makejava
 * @since 2025-07-25 15:27:12
 */
@Service("devLedgerService")
public class DevLedgerServiceImpl implements DevLedgerService {
    @Resource
    private DevLedgerDao devLedgerDao;

    /**
     * 通过ID查询单条数据
     *
     * @param ledId 主键
     * @return 实例对象
     */
    @Override
    public DevLedger queryById(Integer ledId) {
        return this.devLedgerDao.queryById(ledId);
    }

    /**
     * 分页查询
     */
    @Override
    public PageInfo queryAllDevLedgersByPage(PageDto pageDto) {
        //空值处理
        if (pageDto.getPage() == null || pageDto.getSize() == null) {
            pageDto.setPage(1);
            pageDto.setSize(10);
        }
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<DevLedger> list = devLedgerDao.queryAll(pageDto.getDevLedger());
        PageInfo<DevLedger> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param devLedger 实例对象
     * @return 实例对象
     */
    @Override
    public DevLedger insert(DevLedger devLedger) {
        this.devLedgerDao.insert(devLedger);
        return devLedger;
    }

    /**
     * 修改数据
     *
     * @param devLedger 实例对象
     * @return 实例对象
     */
    @Override
    public DevLedger update(DevLedger devLedger) {
        this.devLedgerDao.update(devLedger);
        return this.queryById(devLedger.getLedId());
    }

    /**
     * 通过主键删除数据
     *
     * @param ledId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer ledId) {
        return this.devLedgerDao.deleteById(ledId) > 0;
    }

    /**
     * 批量新增
     * @param devLedgerList
     * @return 新增结果
     */

    @Transactional
    @Override
    public String insertBatch(List<DevLedger> devLedgerList) {
        int count = devLedgerDao.insertBatch(devLedgerList);
        return "新增数据"+count+"条";
    }
}
